package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.dto.ActivityQueryDto;
import com.bjpowernode.crm.workbench.service.ActivityRemarkService;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.vo.ActivityQueryVo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 处理市场活动相关请求
 *
 * @author 刘逸晖
 */
@Getter
@Setter
@RequestMapping
public class ActivityController {

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    /**
     * 处理市场活动相关业务
     */
    private ActivityService activityService;

    /**
     * 处理市场活动评论相关业务
     */
    private ActivityRemarkService activityRemarkService;

    /**
     * 携带所有用户信息跳转至市场活动主页，以供创建时选择所有者
     *
     * @param request 请求
     * @return 市场活动主页
     */
    @GetMapping("/workbench/activity/index.do")
    public ModelAndView index() {
        List<User> userList = this.userService.queryAllUsers();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList", userList)
                .setViewName("workbench/activity/index");
        return modelAndView;
    }

    /**
     * 添加市场活动
     *
     * @param activity 预保存的市场活动
     * @param session  Http 会话
     * @return 结果
     */
    @PostMapping("/workbench/activity/saveCreateActivity.do")
    @ResponseBody
    public ReturnObject addActivity(Activity activity, HttpSession session) {
        return this.activityService.addActivity(activity, session);
    }

    /**
     * 按条件分页查询市场活动
     *
     * @param activityQueryDto 条件
     * @return 结果
     */
    @PostMapping("/workbench/activity/queryActivityByConditionForPage.do")
    @ResponseBody
    public ActivityQueryVo queryActivitiesByConditionsForPage(ActivityQueryDto activityQueryDto) {
        return this.activityService.queryActivitiesByConditionsForPage(activityQueryDto);
    }

    /**
     * 根据 id 列表删除市场活动
     *
     * @param id id 列表
     * @return 删除结果
     */
    @PostMapping("/workbench/activity/deleteActivityIds.do")
    @ResponseBody
    public ReturnObject removeActivitiesByIds(String[] id) {
        this.activityService.removeActivitiesByIds(id);

        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，删除成功");
        return returnObject;
    }

    /**
     * 根据 id 查询市场活动
     *
     * @param id id
     * @return 市场活动
     */
    @PostMapping("/workbench/activity/queryActivityById.do")
    @ResponseBody
    public Activity queryActivityById(String id) {
        return this.activityService.queryActivityById(id);
    }

    /**
     * 编辑市场活动
     *
     * @param activity 市场活动 id 极新数据
     * @param session  http 会话
     * @return 编辑结果
     */
    @PostMapping("/workbench/activity/saveEditActivity.do")
    @ResponseBody
    public ReturnObject editActivityById(Activity activity, HttpSession session) {
        return this.activityService.editActivityById(activity, session);
    }

    /**
     * 下载市场活动数据
     * 空实现
     *
     * @param response 响应
     * @return 错误响应
     * @throws Exception
     */
    @PostMapping("/workbench/activity/fileDownload.do")
    @ResponseBody
    public ReturnObject fileDownload(HttpServletResponse response) throws Exception {
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        returnObject.setMessage("抱歉，该功能暂未实现");
        return returnObject;
    }

    /**
     * 导出所有市场活动数据为 Excel 文件
     * 空实现
     *
     * @param response 响应
     * @return 错误响应
     * @throws Exception
     */
    @PostMapping("/workbench/activity/exportAllActivitys.do")
    @ResponseBody
    public ReturnObject exportAllActivitys(HttpServletResponse response) throws Exception {
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        returnObject.setMessage("抱歉，该功能暂未实现");
        return returnObject;
    }

    /**
     * 上传文件
     * 空实现
     *
     * @param userName 用户名
     * @param myFile   文件
     * @return 错误响应
     * @throws Exception
     */
    @PostMapping("/workbench/activity/fileUpload.do")
    @ResponseBody
    public ReturnObject fileUpload(String userName, MultipartFile myFile) throws Exception {
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        returnObject.setMessage("抱歉，该功能暂未实现");
        return returnObject;
    }

    /**
     * 导入市场活动
     *
     * @param activityFile 文件
     * @param userName     用户名
     * @param session      会话
     * @return 错误响应
     */
    @PostMapping("/workbench/activity/importActivity.do")
    @ResponseBody
    public ReturnObject importActivity(MultipartFile activityFile, String userName, HttpSession session) {
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
        returnObject.setMessage("抱歉，该功能暂未实现");
        return returnObject;
    }

    /**
     * 查询市场活动极其对应的所有评论
     *
     * @param id 市场活动的 id
     * @return 模型和试图
     */
    @GetMapping("/workbench/activity/detailActivity.do")
    public ModelAndView detailActivity(String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("activity", this.activityService.queryActivityById(id))
                .addObject("remarkList", this.activityRemarkService.queryActivityRemarksByActivityId(id))
                .setViewName("workbench/activity/detail");
        return modelAndView;
    }

}
